package com.shizhanzhe.zxbapp.vip;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.CircleListBean;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author YJ
 * @date 2019/3/19
 */
public class HairCircleActivity extends Activity {
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.iv_kecanyu)
    ImageView ivKecanyu;
    @BindView(R.id.tv_kecanyu)
    TextView tvKecanyu;
    @BindView(R.id.iv_zhengzaicanyu)
    ImageView ivZhengzaicanyu;
    @BindView(R.id.tv_zhengzaicanyu)
    TextView tvZhengzaicanyu;
    @BindView(R.id.nodata)
    ImageView nodata;
    private HairCircleAdapter mAdapter;
    private int page = 1;
    private int type = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hair_circle);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.colorAccent)
                .init();
        initData();
        setListener();
    }

    private void initData() {
        topTitle.setText("传播赚");
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new HairCircleAdapter(this);
        recycler.setAdapter(mAdapter);
        updateFq();
    }

    @OnClick({R.id.backarea, R.id.ll_fragment_ge_task_list, R.id.ll_zhengzaicanyuarea})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backarea:
                finish();
                break;
            case R.id.ll_fragment_ge_task_list:
                ivKecanyu.setImageResource(R.mipmap.kcy_s);
                ivZhengzaicanyu.setImageResource(R.mipmap.jxz);
                tvZhengzaicanyu.setTextColor(getResources().getColor(R.color.tv_selector2));
                tvKecanyu.setTextColor(getResources().getColor(R.color.tv_selector));
                type = 0;
                page = 1;
                srl.setVisibility(View.VISIBLE);
                nodata.setVisibility(View.GONE);
                hairCircleList(UserBean.uid, page, type);
                break;
            case R.id.ll_zhengzaicanyuarea:
                ivKecanyu.setImageResource(R.mipmap.kcy);
                ivZhengzaicanyu.setImageResource(R.mipmap.jxz_s);
                tvZhengzaicanyu.setTextColor(getResources().getColor(R.color.tv_selector));
                tvKecanyu.setTextColor(getResources().getColor(R.color.tv_selector2));
                type = 1;
                page = 1;
                srl.setVisibility(View.VISIBLE);
                nodata.setVisibility(View.GONE);
                hairCircleList(UserBean.uid, page, type);
                break;
            default:
                break;
        }
    }

    private void setListener() {
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                hairCircleList(UserBean.uid, page, type);
            }
        });
        srl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                hairCircleList(UserBean.uid, page, type);
            }
        });
        mAdapter.setListener(new HairCircleAdapter.HairCircleListener() {
            @Override
            public void itemClick(CircleListBean.DataBean bean) {
                startActivity(new Intent(HairCircleActivity.this, HairCircleDetailsActivity.class)
                        .putExtra("taskid", bean.getTaskid()));
            }
        });
    }

    private void updateFq(){
        OkHttpDownloadJsonUtil.downloadJson(HairCircleActivity.this, Path.updateFq(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                hairCircleList(UserBean.uid,page,type);
            }
        });
    }

    private void hairCircleList(int uid, int cPage, final int type) {
        OkHttpDownloadJsonUtil.downloadJson(HairCircleActivity.this, Path.quanList(uid,type,cPage), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                srl.finishRefresh();
                srl.finishLoadMore();
                if (!"1".equals(json) && !"2".equals(json)) {
                    Gson gson = new Gson();
                    CircleListBean tiBean = gson.fromJson(json, CircleListBean.class);
                    if (tiBean.getData().size() > 0) {
                        mAdapter.updateData(page, tiBean.getData());
                    } else {
                        if (page == 1) {
                            srl.setVisibility(View.GONE);
                            nodata.setVisibility(View.VISIBLE);
                        } else {
                            Toast.makeText(HairCircleActivity.this, "已加载全部", Toast.LENGTH_SHORT).show();
                        }
                    }
                } else {
                    Toast.makeText(HairCircleActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

}
